package com.erp.erp_servers.Account.imp;

import com.erp.erp_dao.Account.IAccountDAO;
import com.erp.erp_entitys.Account.AccountEntity;
import com.erp.erp_entitys.Account.AccountExample;
import com.erp.erp_servers.Account.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName ImpAccountService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-06-23 12:21
 * @Version 1.0
 */
@Service
public class ImpAccountService implements IAccountService {
    @Autowired
    IAccountDAO accountDAO;
    @Autowired
    IAccountService accountService;
    /**
     * 获取帐号信息
     * @param
     * @return
     */
    @Override
    public AccountEntity selectAccountByCodeandPasswd(String code,String password) {
        AccountExample ae=new AccountExample();
        AccountExample.Criteria ca = ae.createCriteria();
        ca.andUsercodeEqualTo(code);
        ca.andPasswordEqualTo(password);
        List<AccountEntity> list = accountDAO.selectByExample(ae);
        if(list.size()==1){
            return list.get(0);
        }else{
            return null;
        }

    }
    /**
     * 更新帐号信息
     * @param accountEntity
     * @return
     */
    @Override
    @Transactional
    public int updateAccount(AccountEntity accountEntity) {
       // int num = accountDAO.updateByPrimaryKeySelective(accountEntity);
       /* AccountExample example =new AccountExample();
        AccountExample.Criteria criteria = example.createCriteria();*/
        /*if(accountEntity.getCode()!=null && !accountEntity.getCode().equals("")){
            criteria.andCodeEqualTo(accountEntity.getCode());
        }
        if(){

        }*/
     /*   if(accountEntity.getCode() !=null && accountEntity.getUsercode() !=null){
            criteria.andCodeEqualTo(accountEntity.getCode());
        }
        if(accountEntity.getUsercode() !=null && accountEntity.getCode()==null && accountEntity.getPassword() !=null){
            criteria.andUsercodeEqualTo(accountEntity.getUsercode());
        }
*/
       // int num = accountDAO.updateByExampleSelective(accountEntity, example);
       // this.isExist(accountEntity.getUsercode());
        //AccountEntity record, @Param("example") AccountExample example
       /* AccountExample example=new  AccountExample();
        AccountExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(accountEntity.getCode());
        AccountEntity ae=new AccountEntity();
        ae.setUsercode(accountEntity.getUsercode());
        int num = accountDAO.updateByExampleSelective(ae, example);*/
        int num=accountDAO.updateByPrimaryKeySelective(accountEntity);

        return num;
    }
    private void isExist(String usercode){
        AccountEntity accountEntity=new AccountEntity();
        accountEntity.setUsercode(usercode);
        List<AccountEntity> accountEntities = accountService.selectByExample(accountEntity);
        for(int i=0;i<accountEntities.size();i++){
            accountEntity.setUsercode("");
            accountEntity.setName("");
            accountEntity.setCode(accountEntities.get(i).getCode());
            accountEntity.setPassword(accountEntities.get(i).getPassword());
            accountDAO.updateByPrimaryKey(accountEntity);
        }
    }
    /**
     * 获取帐号信息
     *
     * @param accountEntity
     * @return
     */
    @Override
    public List<AccountEntity> selectByExample(AccountEntity accountEntity) {
        AccountExample example =new AccountExample();
        AccountExample.Criteria criteria = example.createCriteria();
        if(accountEntity.getUsercode() !=null && accountEntity.getUsercode() !=""){
            criteria.andUsercodeEqualTo(accountEntity.getUsercode());
        }

        return accountDAO.selectByExample(example);
    }

    /**
     * 获取账号信息
     * @param account 账号查询实体
     * @return 账号信息
     */
    @Override
    public List<AccountEntity> getAccountList(AccountEntity account) {
        return accountDAO.getAccountList(account);
    }
}
